package com.right.health.app.ui.medical;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.TimeUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.MedicalRecordInfo;
import com.right.health.app.net.MedicalApi;
import com.right.health.app.ui.medical.adapter.MedicalRecordAddAdapter;
import com.right.health.app.ui.medical.adapter.MedicalRecordDetailAdapter;
import com.vendor.social.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MedicalRecordDetailActivity extends BaseActivity implements OnHttpListener {

    private static final int HTTP_DETAIL_CODE = 848;
    private static final String BUNDLE_ID_KEY = "id_key";

    private TextView mUserTv, mChooseTimeTv, mChooseTypeTv;
    private ListView mListView;
    private MedicalRecordDetailAdapter mAdapter;
    private MedicalApi mMedicalApi;
    private String[] mVisitTypes, mDiseaseTypes;

    private String mId;//病历id

    private List<MedicalRecordInfo> mMedicalRecordInfos = new ArrayList<>();

    public static void launch(Context context, String id) {
        Intent intent = new Intent(context, MedicalRecordDetailActivity.class);
        if (!TextUtils.isEmpty(id)) {
            Bundle bundle = new Bundle();
            bundle.putString(BUNDLE_ID_KEY, id);
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.medical_record_detail);
    }

    @Override
    public void findView() {
        mUserTv = (TextView) findViewById(R.id.tv_user);
        mChooseTimeTv = (TextView) findViewById(R.id.tv_choose_time);
        mChooseTypeTv = (TextView) findViewById(R.id.tv_choose_type);
        mListView = (ListView) findViewById(R.id.listview);
        mAdapter = new MedicalRecordDetailAdapter(this);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void initialize() {
        Bundle bundle = getIntent().getExtras();
        mMedicalApi = new MedicalApi();
        mMedicalApi.setListener(this);
        mVisitTypes = getResources().getStringArray(R.array.visit_type_arrays);
        mDiseaseTypes = getResources().getStringArray(R.array.course_disease_arrays);
        if (bundle != null && bundle.containsKey(BUNDLE_ID_KEY)) {
            mId = bundle.getString(BUNDLE_ID_KEY);
            mMedicalApi.addRequestCode(HTTP_DETAIL_CODE);
            mMedicalApi.getMedicalDetil(mId);
        }
    }

    private void refreshUI() {
        mChooseTypeTv.setText(mMedicalRecordInfos.get(0).visitName);
        mChooseTimeTv.setText(TimeUtil.convertTime("yyyy-MM-dd",mMedicalRecordInfos.get(0).casehistorytime));
        mAdapter.setDataSource(mMedicalRecordInfos);
    }

    private String getVisitTypeName(String indexStr) {
        String typeName = "";
        try {
            int index = Integer.parseInt(indexStr);
            typeName = mVisitTypes[index];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return typeName;
    }

    private String getDiseaseTypeName(String indexStr) {
        String typeName = "";
        try {
            int index = Integer.parseInt(indexStr);
            typeName = mDiseaseTypes[index];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return typeName;
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {

            if (!TextUtils.isEmpty(response.data)) {
                try {
                    JSONObject jsonObject = new JSONObject(response.data);
                    mMedicalRecordInfos = new Gson().fromJson(jsonObject.getString("casehistory"), new TypeToken<List<MedicalRecordInfo>>() {
                    }.getType());
                    if (!CollectionUtil.isEmpty(mMedicalRecordInfos)) {
                        for (MedicalRecordInfo info : mMedicalRecordInfos) {
                            info.infectionprogressName = getDiseaseTypeName(info.infectionprogresstype);
                            info.visitName = getVisitTypeName(info.visittype);
                            if (!TextUtils.isEmpty(info.casehistorypics)) {
                                String[] pics = info.casehistorypics.split(",");
                                info.photoDataSource = Arrays.asList(pics);
                            }
                        }
                        refreshUI();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("TAG", "onResponse: "+e.getMessage());
                    ToastUtil.show(this, "数据异常");
                }

            } else {
                ToastUtil.show(this, "数据异常");
            }

        }

    }
}
